from re import finditer

if __name__ == '__main__':
    text = "abaaaabbbbbbaaaa"
    pattern = "ab"

    results = finditer(pattern=pattern, string=text)

    # print(type(results))

    for match in results:
        # print(type(match))
        # 匹配的起始位置
        s = match.start()
        # 匹配的终止位置，不包含这个位置
        e = match.end()

        print('Found "{}"\nin "{}"\nfrom {} to {} ("{}")'.format(match.re.pattern, match.string, s, e, text[s:e]))